﻿using SalesSys.DAL;
using SalesSys.DAL.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SalesSys.BLL
{
    public class SalesReportBLL
    {
        //object[][] GetTableView(string sql, bool isHander = true)
        //{
        //    var rep = RepositoryFactory.GetRepositoryByFactoryUnit<IDepotHistroyRep>();
        //    var dt = rep.GetTableInfo(sql);
        //    var list = new List<object[]>();
        //    if (isHander)
        //    {
        //        var cols = dt.Columns.Cast<DataColumn>().Select(p => (object)p.ColumnName).ToArray();
        //        list.Add(cols);
        //    }

        //    var data = dt.Rows.Cast<DataRow>().Select(p => p.ItemArray).ToArray();
        //    list.AddRange(data);
        //    return list.ToArray();
        //}
        ///// <summary>
        ///// 日经营分析报告-采销情况
        ///// </summary>
        ///// <param name="startTime">开始时间（订单结算时间）</param>
        ///// <param name="endTime">结束时间（订单结算时间）</param>
        ///// <param name="providerType">供应商类型</param>
        ///// <returns></returns>
        //public object[][] GetSalesReport(bool isHander, DateTime? startTime, DateTime? endTime, int? providerType)
        //{
        //    var sql = "exec Proc_SalesReport";
        //    if (startTime.HasValue)
        //    {
        //        sql += " @StartCreateTime=" + startTime.Value;
        //    }
        //    if (endTime.HasValue)
        //    {
        //        sql += " @EndCreateTime=" + endTime.Value;
        //    }
        //    if (providerType.HasValue)
        //    {
        //        sql += " @ProviderType=" + providerType.Value;
        //    }
        //    return GetTableView(sql, isHander);
        //}

        public List<SalesReport> GetSalesReportList(bool isHander, DateTime? startTime, DateTime? endTime, int? providerType)
        {
            var sql = "exec Proc_SalesReport";
            if (startTime.HasValue)
            {
                sql += " @StartCreateTime='" + startTime.Value.ToString("yyyy-MM-dd")+"'";
            }
            if (endTime.HasValue)
            {
                if (sql.Contains("@"))
                    sql += ",";
                sql += " @EndCreateTime='" + endTime.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (providerType.HasValue)
            {
                if (sql.Contains("@"))
                    sql += ",";
                sql += " @ProviderType=" + providerType.Value;
            }

            var rep = RepositoryFactory.GetRepositoryByFactoryUnit<IDepotHistroyRep>();
            var dt = rep.GetTableInfo(sql);
            return FillModel(dt);
        }

        /// <summary>  
        /// 填充对象列表：用DataTable填充实体类
        /// </summary>  
        public List<SalesReport> FillModel(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return new List<SalesReport>();
            }
            List<SalesReport> modelList = new List<SalesReport>();
            foreach (DataRow dr in dt.Rows)
            {
                SalesReport model = new SalesReport();
                for (int i = 0; i < dr.Table.Columns.Count; i++)
                {
                    PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                    if (propertyInfo != null && dr[i] != DBNull.Value)
                        propertyInfo.SetValue(model, dr[i], null);
                }

                modelList.Add(model);
            }
            return modelList;
        }
    }
}
